import bean.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/lookStudent")
public class lookStudent extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.创建字符输入流对象，关联文件
        BufferedReader br = new BufferedReader(new FileReader("E:\\stu.txt"));
        //2.创建集合
        ArrayList<Student> list = new ArrayList<>();
        //3.读取数据，封装到Student对象
       String line;
       while ((line= br.readLine())!=null){
           Student stu =new Student();
           String[] arr = line.split(",");
           stu.setUsername(arr[0]);
           stu.setAge(Integer.parseInt(arr[1]));
           stu.setScore(Integer.parseInt(arr[2]));
           list.add(stu);
       }
        //4.将集合对象存入会话领域中
        req.getSession().setAttribute("students",list);
        //5.重定向到学生列表页面
        resp.sendRedirect("/stu/lookStudent.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
